Skip to content

Refactor: Extract Frontend Request Checking Logic#569

Closed
StarkOne wants to merge 3 commits intolaravel:4.xfrom
StarkOne:refactor/frontend-request-checker
Closed

Refactor: Extract Frontend Request Checking Logic#569
StarkOne wants to merge 3 commits intolaravel:4.xfrom
StarkOne:refactor/frontend-request-checker

Conversation

@StarkOne
Copy link

@StarkOne StarkOne commented Jul 1, 2025

This PR refactors the EnsureFrontendRequestsAreStateful middleware to improve its structure and adhere more closely to the Single Responsibility Principle.

Description

The EnsureFrontendRequestsAreStateful middleware was previously responsible for two distinct tasks:

  1. Determining if a request originated from a stateful frontend domain.
  2. Applying the appropriate middleware pipeline based on that determination.

This refactor extracts the domain-checking logic into its own dedicated class, FrontendRequestChecker. This simplifies the middleware, making its sole responsibility to orchestrate the middleware pipeline.

Changes Made

  • New Class: Added a new Laravel\Sanctum\Http\Middleware\FrontendRequestChecker class.
  • Logic Extraction: Moved the logic for parsing referer/origin headers and comparing them against the stateful configuration from EnsureFrontendRequestsAreStateful::fromFrontend() to the new FrontendRequestChecker::isFromFrontend() method.
  • Middleware Simplification: The EnsureFrontendRequestsAreStateful middleware now calls the new checker class. Its internal static method fromFrontend() has been removed.

Benefits of this Change

  • Improved Readability: Each class now has a clear and focused purpose.
  • Single Responsibility Principle: The middleware is no longer concerned with the implementation details of how a request is identified as coming from the frontend.
  • Enhanced Testability: The FrontendRequestChecker can now be unit-tested in complete isolation, leading to simpler and more robust tests.

StarkOne added 3 commits July 1, 2025 08:50
… improve its structure and adhere more closely to the Single Responsibility Principle.
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@StarkOne StarkOne deleted the refactor/frontend-request-checker branch July 1, 2025 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants